<template>
  <div class="app-container">
    <el-form
      :inline="true"
      :model="keyword"
      ref="searchForm"
      class="demo-form-inline"
    >
      <el-form-item label="店铺名称" prop="name">
        <el-input
          size="small"
          clearable
          v-model="keyword.name"
          placeholder="店铺名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="店铺状态">
        <el-select v-model="keyword.status" placeholder="店铺状态">
          <el-option
            v-for="status in statuses"
            :key="status.id"
            :label="status.status"
            :value="status.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-button type="primary" @click="searchEmp()">搜索</el-button>
      <el-button
        type="primary"
        @click="resetSearchForm()"
        icon="el-icon-refresh"
      >
        重置
      </el-button>
      <el-button type="primary" @click="handleAdd()">新增</el-button>
      <el-button type="primary" @click="deleteBatch()">批量删除</el-button>
    </el-form>

    <el-table
      :data="shopList"
      v-loading="listLoading"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
      stripe
      style="width: 100%; margin-top: 10px"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="belongingStoreId" label="id" width="120">
      </el-table-column>
      <el-table-column
        prop="name"
        label="店铺名称"
        width="180"
      ></el-table-column>
      <el-table-column prop="isDefault" label="默认店铺" width="120">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.isDefault == 1"
            :type="scope.row.isDefault == 1 ? 'success' : 'info'"
            disable-transitions
            >是</el-tag
          >
          <el-tag
            v-if="scope.row.isDefault == 0"
            :type="scope.row.isDefault == 1 ? 'success' : 'info'"
            disable-transitions
            >否</el-tag
          >
        </template>
      </el-table-column>
      <el-table-column
        prop="contacts"
        label="联系人"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="phoneNumber"
        label="手机号"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="address"
        label="地址"
        width="140"
      ></el-table-column>
      <el-table-column
        prop="createDate"
        label="创建时间"
        width="180"
      ></el-table-column>
      <el-table-column prop="status" label="店铺状态" width="120">
        <template slot-scope="scope">
          <el-tag
            v-if="scope.row.status == 1"
            :type="scope.row.status == 1 ? 'success' : 'info'"
            disable-transitions
            >启用</el-tag
          >
          <el-tag
            v-if="scope.row.status == 2"
            :type="scope.row.status == 1 ? 'success' : 'info'"
            disable-transitions
            >禁用</el-tag
          >
        </template>
      </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination>

    <!-- 添加对话框 -->
    <el-dialog title="新增店铺" :visible.sync="dialogFormVisible">
      <el-form :model="addFrom">
        <el-form-item label="店铺名称" :label-width="formLabelWidth">
          <el-input v-model="addFrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="联系人" :label-width="formLabelWidth">
          <el-input v-model="addFrom.contacts" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="addFrom.phoneNumber" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="addFrom.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add()">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 修改对话框 -->
    <el-dialog title="新增店铺" :visible.sync="dialogUpdateFormVisible">
      <el-form :model="updateFrom">
        <el-form-item label="店铺名称" :label-width="formLabelWidth">
          <el-input v-model="updateFrom.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="默认店铺" :label-width="formLabelWidth">
          <el-radio v-model="updateFrom.isDefault" label="1">是</el-radio>
          <el-radio v-model="updateFrom.isDefault" label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="联系人" :label-width="formLabelWidth">
          <el-input v-model="updateFrom.contacts" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input
            v-model="updateFrom.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="updateFrom.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="店铺状态" :label-width="formLabelWidth">
          <el-select v-model="updateFrom.status" placeholder="请选择">
            <el-option
              v-for="status in statuses"
              :key="status.id"
              :label="status.status"
              :value="status.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogUpdateFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  shopListPage,
  deleteShop,
  deleteShopBatch,
  addShop,
  shopInfo,
  updateShop,
} from "@/api/shopList";

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "gray",
        deleted: "danger",
      };
      return statusMap[status];
    },
  },
  data() {
    return {
      shopList: [],
      listLoading: true,
      dialogFormVisible: false,
      dialogUpdateFormVisible: false,
      keyword: {
        name: "",
        status: "",
      },
      addFrom: {
        name: "",
        contacts: "",
        phoneNumber: "",
        address: "",
      },
      updateFrom: {
        belongingStoreId: "",
        name: "",
        isDefault: "",
        contacts: "",
        phoneNumber: "",
        address: "",
        createDate: "",
        status: "",
      },
      formLabelWidth: "120px",
      currentPage: 1,
      total: 0,
      pageSize: 5,
      ids: [],
      statuses: [
        {
          id: 1,
          status: "启用",
        },
        {
          id: 2,
          status: "禁用",
        },
      ],
    };
  },
  created() {
    this.fetchData();
  },
  methods: {
    fetchData() {
      this.listLoading = false;
      shopListPage({
        name: this.keyword.name,
        status: this.keyword.status,
        pageNum: this.currentPage,
        pageSize: this.pageSize,
      }).then((response) => {
        this.shopList = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    searchEmp() {
      this.fetchData();
    },
    handleAdd() {
      this.addFrom = {};
      this.dialogFormVisible = true;
    },
    // 添加
    add() {
      addShop(this.addFrom).then((response) => {
        if (response.code == 200) {
          this.dialogFormVisible = false;
          this.fetchData();
        }else {
          this.$message({
            message: response.msg,
            type: "error",
            duration: 1000
          })
        }
      });
    },
    // 修改
    handleEdit(index, row) {
      shopInfo(row.belongingStoreId).then((res) => {
        this.updateFrom.belongingStoreId = res.data.belongingStoreId;
        this.updateFrom.name = res.data.name;
        this.updateFrom.isDefault = res.data.isDefault + "";
        this.updateFrom.contacts = res.data.contacts;
        this.updateFrom.phoneNumber = res.data.phoneNumber;
        this.updateFrom.address = res.data.address;
        this.updateFrom.status = res.data.status;
      });
      this.dialogUpdateFormVisible = true;
    },
    update() {
      updateShop(this.updateFrom).then((response) => {
        if (response.code == 200) {
          this.dialogUpdateFormVisible = false;
          this.fetchData();
        }
      });
    },
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteShop(row.belongingStoreId).then((response) => {
            if (response.code == 200) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.fetchData();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 每页显示的记录数发生变化触发的操作
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.fetchData();
    },
    // 当前页发生变化触发的操作
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.fetchData();
    },
    handleSelectionChange(val) {
      this.ids = [];
      for (var i = 0; i < val.length; i++) {
        this.ids.push(val[i].belongingStoreId);
      }
    },
    deleteBatch() {
      // 提示，自行处理
      if (this.ids.length == 0) {
        alert("请选择至少一条数据");
        return;
      }
      deleteShopBatch(this.ids.join(",")).then((response) => {
        if (response.code == 200) {
          this.$message({
            message: "批量删除成功",
            type: "success",
            duration: 1000,
          });
          this.fetchData();
        } else {
          this.$message({
            message: "批量删除失败",
            type: "error",
            duration: 1000,
          });
        }
      });
    },
    resetSearchForm() {
      this.keyword = {};
      this.fetchData();
    },
  },
};
</script>
